package com.card.controller;

import com.card.service.WeChatAuthService;
import com.card.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * 交互微信接口
 */
@Slf4j
@RestController
@RequestMapping("/wx")
public class WeChatAuthController {

    @Resource
    private WeChatAuthService weChatAuthService;

    /**
     * 根据code获取微信用户信息
     * @param requestBody code
     * @return
     * @throws IOException
     */
    @PostMapping("/login")
    public Result<Map<String, Object>> wxLogin(@RequestBody Map<String, String> requestBody) throws IOException {

        String code = requestBody.get("code");

        if (code == null || code.isEmpty()) {
            return Result.error("code不能为空");
        }
        Map<String, Object> map = weChatAuthService.wxLogin(code);
        return Result.success(map);
    }
}
